package Leetcode;

public class problem24 {
    public ListNode swapPairs(ListNode head) {
        if(head == null || head.next == null){
            return head;
        }

        ListNode newRoot = head.next;
        ListNode pre = new ListNode(0);
        while(head != null && head.next != null){
            ListNode next = head.next;
            ListNode twoNext = next.next;
            pre.next = next;
            next.next = head;
            head.next = twoNext;
            pre = head;
            head = head.next;
        }

        return newRoot;
    }

    public class ListNode {
        int val;
        ListNode next;
        ListNode(int x) { val = x; }
    }

}
